package us.bighelp2.marc.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class AppDBConnection
{
	Connection con;
	private static Logger logger = Logger.getLogger(AppDBConnection.class);

	public AppDBConnection(String databaseName)
	{
		try
		{
			String SQLDrive = "com.mysql.jdbc.Driver";
			String SQLURL = "jdbc:mysql://localhost:3306/" + databaseName;
			String SQLUser = "marc";
			String SQLPw = "Tru5tN01!";
			
			Class.forName(SQLDrive).newInstance();
			Properties pr = new Properties();
			pr.setProperty("user", SQLUser);
			pr.setProperty("password", SQLPw);
			
			try
			{
				con = DriverManager.getConnection(SQLURL,pr);
			}
			catch(SQLException se)
			{
				se.printStackTrace();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			finally
			{
			}
		}
		catch (Exception e)
		{
			if(logger.isDebugEnabled()){logger.debug("Failed to load MySQL Driver" + e);}
		}
	}
	
	public Connection getConnection()
	{
		return this.con;
	}
	
	public void closeConnection()
	{
		try
		{
			this.con.close();
		}
		catch (SQLException e)
		{
			if(logger.isDebugEnabled()){logger.debug("SQL Error:" + e);}
			SQLException e1 = ((SQLException)e).getNextException();
			if (e1 != null)
				if(logger.isDebugEnabled()){logger.debug("SQL Error:" + e1);}
		}
	}
}
